Bit Manipulation for Competitive Programming
Bit manipulation is a technique in competitive programming that involves the manipulation of individual bits in binary representations of numbers. It is a valuable technique in competitive programming because it allows you to solve problems efficiently, often reducing time complexity and memory usage....
read more
Number with set bits only between L-th and R-th index
Given L and R. The task is to find the number in whose binary representation all bits between the L-th and R-th index are set and the rest of the bits are unset. The binary representation is 32 bits. Examples:...
read more
Minimize the number of steps required to reach the end of the array
Given an integer array arr[] of length N consisting of positive integers, the task is to minimize the number of steps required to reach the index ‘N-1’. At a given step if we are at index ‘i’ we can go to index ‘i-arr[i]’ or ‘i+arr[i]’ given we have not visited those indexes before. Also, we cannot go outside the bounds of the array. Print -1 if there is not possible way.Examples:...
read more
Represent N as the sum of exactly K powers of two | Set 3
Given two integers N and K, the task is to find whether it is possible to represent N as the sum of exactly K powers of 2. If possible, then print K positive integers such that they are powers of 2 and their sum is exactly equal to N. Otherwise, print “Impossible”. If multiple answers exist, print any....
read more
Print steps to make a number in form of 2^X – 1
Given a number N, there are two steps to be performed....
read more
Smallest number exceeding N whose Kth bit is set
Given two integers N and K, the task is to find the smallest number greater than N whose Kth bit in its binary representation is set....
read more
Sort array by performing swapping under certain condition
Given an array of positive elements arr[] of size N. You can select any index i such that i + 1 < N and swap arr[i], arr[i + 1] if they have the same number of set bits in their binary representation. The task is to determine whether it is possible to sort the array in ascending order by swapping any number of times. If it is possible, you should return true, otherwise return false....
read more
Inserting M into N such that m starts at bit j and ends at bit i | Set-2
Given two 32-bit numbers, N and M, and two-bit positions, i and j. Write a method to insert M into N such that M starts at bit j and ends at bit i. You can assume that the bits j through i have enough space to fit all of M. Assuming index start from 0.Examples:...
read more
Smallest number greater or equals to N such that it has no odd positioned bit set
Given an integer N, the task is to find the smallest integer X such that it has no odd position set and X ? N. Note: The positioning of bits is assumed from the right side and the first bit is assumed to be the 0th bit....
read more
Unset least significant K bits of a given number
Given an integer N, the task is to print the number obtained by unsetting the least significant K bits from N....
read more
Subtraction of two numbers using 2’s Complement
Given two numbers a and b. The task is to subtract b from a by using 2’s Complement method.Note: Negative numbers represented as 2’s Complement of Positive Numbers.For example, -5 can be represented in binary form as 2’s Complement of 5. Look at the image below:...
read more
Count numbers in the range [L, R] having only three set bits
Given an array arr[] of N pairs, where each array element denotes a query of the form {L, R}, the task is to find the count of numbers in the range [L, R], having only 3-set bits for each query {L, R}....
read more